今天來講講本地推播
1.先導入推播
import UserNotifications
2.先獲取權限
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
}//權限的部分
3.接下來寫入下面的程式
let content = UNMutableNotificationContent()
content.title = "Hey"
content.body = "Look at me!" //內容
let date = Date().addingTimeInterval(5) //延遲時間
let dateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: date)//取系統時間
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)//觸發條件
let uuidString = UUID().uuidString
let request = UNNotificationRequest(identifier: uuidString, content: content, trigger: trigger)//設置要求
center.add(request) { (error) in//導入要求
}
}
4.成果如下